#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  };
 
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> ret;
        if (root == nullptr)
        {
            return ret;
        }
        queue<TreeNode*> as;
        as.push(root);
        while (as.size())
        {
            int q = as.size();
            int s = INT_MIN;
            for (int i = 0; i < q; i++)
            {
                TreeNode* w = as.front();
                as.pop();
                s = max(s, w->val);
                if (w->left)
                {
                    as.push(w->left);
                }
                if (w->right)
                {
                    as.push(w->right);
                }
            }
            ret.push_back(s);
        }
        return ret;
    }
};